﻿namespace ArienToolkits.Caching
{
    /// <summary>
    /// 缓存处理基础接口
    /// </summary>
    public interface ICache
    {
        /// <summary>
        /// 缓存对象
        /// <remarks>如果缓存键下没有值，则添加新值；如果缓存键下有值，则更新改值</remarks>
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <param name="obj">缓存值</param>
        /// <param name="addSqlDependency">是否添加SQL缓存依赖项</param>
        void Save(string key, object obj, bool addSqlDependency = false);

        /// <summary>
        /// 从缓存中获取对象
        /// </summary>
        /// <typeparam name="T">缓存对象类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <returns></returns>
        T Get<T>(string key);

        /// <summary>
        /// 从缓存中获取对象
        /// </summary>
        /// <typeparam name="T">缓存对象类型</typeparam>
        /// <param name="key">缓存键</param>
        /// <param name="action">缓存不存在时的缓存动作</param>
        /// <param name="addSqlDependency">是否添加SQL缓存依赖项</param>
        /// <returns></returns>
        T Get<T>(string key, System.Func<object> action, bool addSqlDependency = false);

        /// <summary>
        /// 移除指定缓存项
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>被缓存对象</returns>
        object Remove(string key);

        /// <summary>
        /// 清空缓存
        /// </summary>
        void Clear();

        /// <summary>
        /// 获取缓存对象
        /// </summary>
        /// <param name="key">缓存键</param>
        /// <returns>缓存值</returns>
        object this[string key] { get; }
    }
}
